﻿using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Crypto.Agreement.JPake;
using ReportMealManagementSystem.Business;
using ReportMealManagementSystem.Entity;
using ReportMealManagementSystem.Tool;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ReportMealManagementSystem
{
    /// <summary>
    /// SystemSetting.xaml 的交互逻辑
    /// </summary>
    public partial class SystemSetting : UserControl
    {
        Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        int companyID = int.Parse(ConfigurationManager.AppSettings["companyID"]);
        int districtID = int.Parse(ConfigurationManager.AppSettings["districtID"]);
        BasicInfo basicInfo = new BasicInfo();
        bool isConstraintDishes = bool.Parse(ConfigurationManager.AppSettings["isConstraintDishes"]);
        int? constraintDishesID = null;

        public SystemSetting()
        {
            InitializeComponent();
        }

        private void GetCompanyInfo()
        {
            string msg = "";
            JObject joParam = new JObject();
            joParam["PageIndex"] = 1;
            joParam["PageSize"] = 1000;
            int totalNumber = 0;
            int totalPage = 0;
            var CompanyList = basicInfo.GetData(joParam, "gbt/BasicInfo/CompanyList", "公司", ref totalNumber, ref totalPage, ref msg);
            if (CompanyList != null)
            {
                JObject joItem = new JObject();
                List<SelectedItem> selectedItems = new List<SelectedItem>();
                for (int index = 0; index < CompanyList.Count; index++)
                {
                    joItem = (JObject)CompanyList[index];
                    selectedItems.Add(new SelectedItem
                    {
                        ID = (int)CompanyList[index]["id"],
                        Name = (string)CompanyList[index]["name"]
                    });
                }

                cbCompany.Dispatcher.Invoke(new Action(delegate
                {
                    cbCompany.ItemsSource = selectedItems;
                    cbCompany.SelectedValue = companyID;
                }));
            }
        }

        private void GetDistrictInfo()
        {
            string msg = "";
            JObject joParam = new JObject();
            joParam["PageIndex"] = 1;
            joParam["PageSize"] = 1000;
            joParam["CompanyID"] = companyID;
            int totalNumber = 0;
            int totalPage = 0;
            var DistrictList = basicInfo.GetData(joParam, "gbt/BasicInfo/DistrictList/", "区域", ref totalNumber, ref totalPage, ref msg);
            if (DistrictList != null)
            {
                JObject joItem = new JObject();
                List<SelectedItem> selectedItems = new List<SelectedItem>();
                for (int index = 0; index < DistrictList.Count; index++)
                {
                    joItem = (JObject)DistrictList[index];
                    selectedItems.Add(new SelectedItem
                    {
                        ID = (int)DistrictList[index]["districtID"],
                        Name = (string)DistrictList[index]["districtName"]
                    });
                }

                cbDistrict.Dispatcher.Invoke(new Action(delegate
                {
                    cbDistrict.ItemsSource = selectedItems;
                    cbDistrict.SelectedValue = districtID;
                }));
            }
        }

        private void GetSetting()
        {
            var setting = basicInfo.GetSystemSettingInfo(districtID);
            if (setting.Property("SystemSetting") != null)
            {
                JArray jaResult = (JArray)setting["SystemSetting"];

                JObject joItem = new JObject();
                JObject joConstraintDishes = new JObject();
                for (int index = 0; index < jaResult.Count; index++)
                {
                    joItem = (JObject)jaResult[index];
                    //强制点菜
                    if ((string)joItem["name"] == "ConstraintDishes")
                    {
                        joConstraintDishes = joItem;
                    }
                }
                bool constraintDishesValue = (bool)joConstraintDishes["value"];
                constraintDishesID = (int)joConstraintDishes["id"];
                cbIsConstraintDishes.Dispatcher.Invoke(new Action(delegate
                {
                    if (constraintDishesValue)
                    {
                        cbIsConstraintDishes.IsChecked = true;
                    }
                    else
                    {
                        cbIsConstraintDishes.IsChecked = false;
                    }
                }));
            }
        }

        private void SetSetting()
        {
            string message = "";
            JObject joParam = new JObject();
            joParam["id"] = constraintDishesID;
            joParam["value"] = isConstraintDishes.ToString();
            bool result = basicInfo.OptDataInfo(joParam, "gbt/SystemSetting/ConstraintDishes", ref message);
            this.message.Dispatcher.Invoke(new Action(delegate {
                this.message.Content = message;
            }));
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Task task = new Task(new Action(delegate
            {
                companyID = int.Parse(cfa.AppSettings.Settings["companyID"].Value);
                districtID = int.Parse(cfa.AppSettings.Settings["districtID"].Value);
                isConstraintDishes = bool.Parse(cfa.AppSettings.Settings["isConstraintDishes"].Value);
                GetCompanyInfo();
            }));
            task.Start();
            task.ContinueWith((t) =>
            {

            });
        }

        private void cbCompany_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbCompany.SelectedValue != null)
            {
                cfa.AppSettings.Settings["companyID"].Value = cbCompany.SelectedValue.ToString();
                cfa.Save();
                companyID = (int)cbCompany.SelectedValue;
                Task task = new Task(new Action(delegate
                {
                    GetDistrictInfo();
                }));
                task.Start();
                task.ContinueWith((t) =>
                {

                });
            }
        }

        private void cbDistrict_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbDistrict.SelectedValue != null)
            {
                cfa.AppSettings.Settings["districtID"].Value = cbDistrict.SelectedValue.ToString();
                cfa.Save();
                districtID = (int)cbDistrict.SelectedValue;
                Task task = new Task(new Action(delegate
                {
                    GetSetting();
                }));
                task.Start();
                task.ContinueWith((t) =>
                {

                });
            }
        }

        private void cbIsConstraintDishes_Click(object sender, RoutedEventArgs e)
        {
            if (cbIsConstraintDishes.IsChecked == true)
            {
                isConstraintDishes = true;
                cfa.AppSettings.Settings["isConstraintDishes"].Value = "true";
            }
            else
            {
                isConstraintDishes = false;
                cfa.AppSettings.Settings["isConstraintDishes"].Value = "false";
            }
            Task task = new Task(new Action(delegate
            {
                SetSetting();
            }));
            task.Start();
            task.ContinueWith((t) =>
            {

            });
            cfa.Save();
        }
    }
}
